Skip to contentMethod: IndirectSet.IndirectSetIterator(IndirectSet, Iterator)
1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.adapters;
19:
20: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
21:
22: import java.lang.reflect.Field;
23: import java.util.*;
24:
25: public class IndirectSet<E> extends IndirectCollection<Set<E>> implements Set<E> {
26:
27: private final Set<E> internalSet;
28:
29: /**
30: * No-arg constructor to allow clone building.
31: */
32: IndirectSet() {
33: this.internalSet = new HashSet<>();
34: }
35:
36: public IndirectSet(Object owner, Field f, UnitOfWorkImpl uow, Set<E> referencedSet) {
37: super(owner, f, uow);
38: this.internalSet = Objects.requireNonNull(referencedSet);
39: }
40:
41: @Override
42: public int size() {
43: return internalSet.size();
44: }
45:
46: @Override
47: public boolean isEmpty() {
48: return internalSet.isEmpty();
49: }
50:
51: @Override
52: public boolean contains(Object o) {
53: return internalSet.contains(o);
54: }
55:
56: @Override
57: public Iterator<E> iterator() {
58: return new IndirectSetIterator<>(internalSet.iterator());
59: }
60:
61: @Override
62: public Object[] toArray() {
63: return internalSet.toArray();
64: }
65:
66: @Override
67: public <T> T[] toArray(T[] a) {
68: return internalSet.toArray(a);
69: }
70:
71: @Override
72: public boolean add(E e) {
73: boolean res = internalSet.add(e);
74: if (res) {
75: persistChange();
76: }
77: return res;
78: }
79:
80: @Override
81: public boolean remove(Object o) {
82: boolean res = internalSet.remove(o);
83: if (res) {
84: persistChange();
85: }
86: return res;
87: }
88:
89: @Override
90: public boolean containsAll(Collection<?> c) {
91: return internalSet.containsAll(c);
92: }
93:
94: @Override
95:
96: public boolean addAll(Collection<? extends E> c) {
97: boolean res = internalSet.addAll(c);
98: if (res) {
99: persistChange();
100: }
101: return res;
102: }
103:
104: @Override
105: public boolean retainAll(Collection<?> c) {
106: boolean res = internalSet.retainAll(c);
107: if (res) {
108: persistChange();
109: }
110: return res;
111: }
112:
113: @Override
114: public boolean removeAll(Collection<?> c) {
115: boolean res = internalSet.removeAll(c);
116: if (res) {
117: persistChange();
118: }
119: return res;
120: }
121:
122: @Override
123: public void clear() {
124: internalSet.clear();
125: persistChange();
126: }
127:
128: private class IndirectSetIterator<T> implements Iterator<T> {
129:
130: private final Iterator<T> iterator;
131:
132: private IndirectSetIterator(Iterator<T> iterator) {
133: this.iterator = iterator;
134: }
135:
136: @Override
137: public boolean hasNext() {
138: return iterator.hasNext();
139: }
140:
141: @Override
142: public T next() {
143: return iterator.next();
144: }
145:
146: @Override
147: public void remove() {
148: iterator.remove();
149: IndirectSet.this.persistChange();
150: }
151: }
152:
153: @Override
154: public Set<E> unwrap() {
155: return internalSet;
156: }
157:
158: @Override
159: public boolean equals(Object o) {
160: if (o instanceof Set) {
161: if (o instanceof IndirectSet) {
162: return internalSet.equals(((IndirectSet) o).internalSet);
163: }
164: return internalSet.equals(o);
165: }
166: return false;
167: }
168:
169: @Override
170: public int hashCode() {
171: return internalSet.hashCode();
172: }
173:
174: @Override
175: public String toString() {
176: return internalSet.toString();
177: }
178: }